package com.jimmy.medical.controller;

import com.jimmy.medical.common.StringUtils;
import com.jimmy.medical.entity.FlowOrder;
import com.jimmy.medical.entity.Order;
import com.jimmy.medical.entity.User;
import com.jimmy.medical.model.*;
import com.jimmy.medical.service.IFlowOrderService;
import com.jimmy.medical.service.IOrderService;
import com.jimmy.medical.service.IUserService;
import com.jimmy.medical.util.AjaxUtils;
import com.jimmy.medical.util.Func_T;
import com.jimmy.medical.util.Func_T1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/middle")
public class MiddleController {
    @Autowired
    @Qualifier("flowOrderService")
    private IFlowOrderService flowOrderService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;

    /**
     * 待办事项统计
     * @param hospitalId
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public AjaxResult<String> login(final String userName, final String passWord) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                if(StringUtils.isBlank(userName) || StringUtils.isBlank(passWord)) {
                    return "400";  //用户名或密码为空
                } else {
                    User user = userService.checkUser(userName, passWord);
                    if(user == null) {
                        return "404";  //用户名或密码错误
                    }
                }
                return "200";
            }
        });
    }

    /**
     * 待办事项统计
     * @param hospitalId
     * @return
     */
    @RequestMapping("/taskList")
    @ResponseBody
    public AjaxResult<List<UserTask>> taskList(final Long hospitalId) {
        return AjaxUtils.process(new Func_T<List<UserTask>>() {
            @Override
            public List<UserTask> invoke() throws Exception {
                return flowOrderService.getAllTask(hospitalId);
            }
        });
    }

    /**
     * 待办事项列表
     * @param page
     * @param flowOrderTypeId
     * @return
     */
    @RequestMapping("/unassignedFlowOrderList")
    @ResponseBody
    public PageResponseResult<FlowOrder> unassignedFlowOrderList(PageRequestParam page, final Long flowOrderTypeId, final Long orderTypeId) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return AjaxUtils.processPage(page, sort, new Func_T1<PageRequest, Page<FlowOrder>>() {
            @Override
            public Page<FlowOrder> invoke(PageRequest pageRequest) throws Exception {
                return flowOrderService.findAll(pageRequest, null, flowOrderTypeId, orderTypeId, null, null, (short)0, null);
            }
        });
    }

    /**
     * 所有订单
     * @param page
     * @param queryParam
     * @return
     */
    @RequestMapping("/orderList")
    @ResponseBody
    public PageResponseResult<Order> orderList(PageRequestParam page, final OrderQueryParam queryParam) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return AjaxUtils.processPage(page, sort, new Func_T1<PageRequest, Page<Order>>() {
            @Override
            public Page<Order> invoke(PageRequest pageRequest) throws Exception {
                Page<Order> orderPage = orderService.findAll(pageRequest, queryParam);

                return orderPage;
            }
        });
    }

    /**
     * 所有用户
     * @param page
     * @param city
     * @param hospital
     * @param realName
     * @return
     */
    @RequestMapping("/userList")
    @ResponseBody
    public PageResponseResult<UserStat> userList(PageRequestParam page, final String city, final Long hospital, final String realName) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return AjaxUtils.processPage(page, sort, new Func_T1<PageRequest, Page<UserStat>>() {
            @Override
            public Page<UserStat> invoke(PageRequest pageRequest) throws Exception {
                return userService.findUserWithStat(pageRequest, city, hospital, realName);
            }
        });
    }

    /**
     * 用户待办事项
     * @param userId
     * @return
     */
    @RequestMapping("/userFlowOrderList")
    @ResponseBody
    public AjaxResult<List<FlowOrder>> userFlowOrderList(final Long userId) {
        return AjaxUtils.process(new Func_T<List<FlowOrder>>() {
            @Override
            public List<FlowOrder> invoke() throws Exception {
                List<FlowOrder> flowOrderList = flowOrderService.getUserTask(userId);
                for(FlowOrder flowOrder : flowOrderList) {
                    flowOrder.setCountDown(flowOrderService.getCountDownInternal(flowOrder));
                }

                return flowOrderList;
             }
        });
    }

    @RequestMapping("/assign")
    @ResponseBody
    public AjaxResult<String> assign(final Long flowOrderId, final Long userId) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                flowOrderService.saveAssignInfo(flowOrderId, userId);
                return "200";
            }
        });
    }

    /**
     * 中台退号 - 生成退号工单并指派服务人员进行退号
     * @param orderId  订单di
     * @param userId  退号工单指派的用户id
     * @return
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
    public AjaxResult<String> cancelOrder(final Long orderId, final Long userId) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                flowOrderService.cancelOrderFromMiddle(orderId, userId);
                return "200";
            }
        });
    }

    /**
     * 中台改号 - 生成改号工单并指派服务人员进行改号
     * @param orderId  订单di
     * @param userId  改号工单指派的用户id
     * @return
     */
    @RequestMapping("/reSignOrder")
    @ResponseBody
    public AjaxResult<String> reSignOrder(final Long orderId, final Long userId) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                flowOrderService.reSignOrderFromMiddle(orderId, userId);
                return "200";
            }
        });
    }
}
